/**
 * @fileoverview dragscroll - scroll area by dragging
 * @version 0.0.4 micro
 *
 * @license MIT, see http://github.com/asvd/intence
 * @copyright 2015 asvd <heliosframework@gmail.com>
 */

var isDrag = 0;
var lastClientX, lastClientY, pushed;
var elems;
var elem;

$(function () {
    elems = document.getElementsByClassName('dragscroll');
    elem = elems.item(0);
})

function drag() {
    var addEventListener = 'addEventListener';
    var removeEventListener = 'removeEventListener';

    if (isDrag == 0) {
        isDrag = 1;
        elem.style.cursor = "-webkit-grab";
        elem.style.cursor = "-moz-grabbing";
        elem.style.cursor = "-o-grab";
        elem.style.cursor = "grab";

        elem[addEventListener]('mousedown', a1, 0);
        window[addEventListener]('mousemove', a2, 0);
        window[addEventListener]('mouseup', a3, 0);
        $(".fwr_toobar_handtool").addClass("fwr_toobar_handtool_active");
    } else {
        isDrag = 0;
        elem.style.cursor = "-webkit-auto";
        elem.style.cursor = "-moz-auto";
        elem.style.cursor = "-o-auto";
        elem.style.cursor = "auto";

        elem[removeEventListener]('mousedown', a1, 0);
        window[removeEventListener]('mousemove', a2, 0);
        window[removeEventListener]('mouseup', a3, 0);
        $(".fwr_toobar_handtool").removeClass("fwr_toobar_handtool_active");
    }
}
function a1(e) {
    pushed = 1;
    lastClientX = e.clientX;
    lastClientY = e.clientY;

    e.preventDefault();
    e.stopPropagation();
}

function a2(e) {
    if (pushed) {
        elem.scrollLeft -=
            (-lastClientX + (lastClientX = e.clientX));
        elem.scrollTop -=
            (-lastClientY + (lastClientY = e.clientY));
    }
}

function a3() {
    pushed = 0;
}